package bdddaosimpl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import bdd.beans.Contratquadriennal;
import bdd.beans.DBLink;
import bdddaos.DAOContratquadriennal;

//OK
public class DAOContratquadriennalImpl implements DAOContratquadriennal{
	private DBLink dblink;

	public DAOContratquadriennalImpl() {		
		super();
		this.dblink = new DBLink(); 
	}
	
	
	public Contratquadriennal save(Contratquadriennal o) {
		try {
			this.dblink.connexion();
			DateFormat format = new SimpleDateFormat("dd-MM-yy");
			String r = "INSERT INTO CONTRATQUADRIENNAL (DATECONTRAT, DUREE)" +
			" VALUES ('"+format.format(o.getDatecontrat())+ "', "+ o.getDuree() +")";
			this.dblink.setPst(this.dblink.getConnection().prepareStatement(r));
			this.dblink.getPst().executeQuery();
			DAOOutils outil = new DAOOutils();
			o.setNumcontrat(outil.findMaxId("CONTRATQUADRIENNAL", "NUMCONTRAT"));
		}
		catch(SQLException se) {
			se.printStackTrace();
		}
		finally{
			this.dblink.deconnexion();
		}
		return o;
	}

	public Contratquadriennal find(Integer id) {
		try {
			this.dblink.connexion();
			String r = "SELECT * FROM CONTRATQUADRIENNAL WHERE NUMCONTRAT = " + id;
			this.dblink.setPst(this.dblink.getConnection().prepareStatement(r));
			ResultSet rs = this.dblink.getPst().executeQuery();
			if(rs.next()) {
				Contratquadriennal retour = new Contratquadriennal();
				retour.setNumcontrat(id);
				retour.setDatecontrat(rs.getDate("DATECONTRAT"));
				retour.setDuree(rs.getInt("DUREE"));
				
				this.dblink.getPst().close();
				this.dblink.deconnexion();
				
				return retour;
			}		
		}
		catch(SQLException se) {
			System.out.println("Erreur de requ�te" + se);
		}
		finally{
			this.dblink.deconnexion();
		}
		return null;
	}

	public List<Contratquadriennal> findAll() {
		List<Contratquadriennal> Lstretour = new ArrayList<Contratquadriennal>();
		try {
			this.dblink.connexion();
			this.dblink.setPst(this.dblink.getConnection().prepareStatement("SELECT * FROM CONTRATQUADRIENNAL"));
			ResultSet rs = this.dblink.getPst().executeQuery();
			while(rs.next()) {
				Contratquadriennal retour = new Contratquadriennal();
				retour.setNumcontrat(rs.getInt("NUMCONTRAT"));
				retour.setDatecontrat(rs.getDate("DATECONTRAT"));
				retour.setDuree(rs.getInt("DUREE"));
				Lstretour.add(retour);
			}		
		}
		catch(SQLException se) {
			System.out.println("Erreur de requ�te" + se);
		}
		finally{
			this.dblink.deconnexion();
		}
		return Lstretour;
	}

	public void update(Contratquadriennal o) {
		try {
			this.dblink.connexion();
			DateFormat format = new SimpleDateFormat("dd-MM-yy");
			String r = "UPDATE CONTRATQUADRIENNAL SET DATECONTRAT='"+format.format(o.getDatecontrat())+ "', DUREE='"+o.getDuree() + "' WHERE NUMCONTRAT="+o.getNumcontrat();
			
			this.dblink.setPst(this.dblink.getConnection().prepareStatement(r));
			this.dblink.getPst().executeQuery();
		}
		catch(SQLException se) {
			System.out.println("Erreur de requ�te : " + se);
		}
		finally{
			this.dblink.deconnexion();
		}
	}

	public void delete(Contratquadriennal o) {
		//TODO DELETE RECURSIF ?
		try {
			this.dblink.connexion();
			String r = "DELETE FROM CONTRATQUADRIENNAL WHERE NUMCONTRAT="+o.getNumcontrat();
			
			this.dblink.setPst(this.dblink.getConnection().prepareStatement(r));
			this.dblink.getPst().executeQuery();
		}
		catch(SQLException se) {
			System.out.println("Erreur de requ�te : " + se);
		}
		finally{
			this.dblink.deconnexion();
		}
	}
}

